package com.dmbjz.coredemo.controller;


import com.dmbjz.coredemo.event.EventPublisher;
import com.dmbjz.coredemo.event.LoginSuccessEvent;
import com.dmbjz.coredemo.po.UserPo;
import com.dmbjz.coredemo.service.AccountService;
import com.dmbjz.coredemo.service.CouponService;
import com.dmbjz.coredemo.service.SysService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Resource
    private AccountService accountService;

    @Resource
    private CouponService couponService;

    @Resource
    private SysService sysService;

    @Resource
    private EventPublisher eventPublisher;


    /** 使用非事件驱动方式开发，需要手动调用服务 */
    @GetMapping("login")
    public String login(String username,String password){

        //第一步：账号服务自动签到
        accountService.addAccount(username);

        //第二步：优惠服务随机发放优惠卷
        couponService.addCoupon(username);

        //第三步：系统服务对用户登录信息进行记录
        sysService.recordLog(username);

        return username + "登录成功";

    }



    /** 使用事件驱动方式开发，符合设计模式（对修改关闭，对新增打开） */
    @GetMapping("login2")
    public String login2(String username,String password){

        //发布事件
        UserPo userPo = new UserPo(username,password);
        LoginSuccessEvent loginEvent = new LoginSuccessEvent(userPo);
        eventPublisher.sendEvent(loginEvent);

        return username + "登录成功";

    }



}
